# Author:: Alexandre Ribeiro de Souza (<alexandre@lumis.com.br>)

include_recipe "lumis_users::systemuser"

user = "tomcat" #node['authorization']['system']['user']
aws = data_bag_item("aws", "knife-user")
client = data_bag_item("client", Chef::Config[:node_name].split('-')[1])
lumisportal_dir = node['lumis']['lumisportal']['dir']

unless File.exists?(node['lumis']['lumisportal']['home'])
  directory node['lumis']['lumisportal']['home'] do
    group user
    owner user
    mode "0755"
    recursive true
  end
  
  lumisportal_file = node['lumis']['lumisportal']['remote_path'].split('/')[-1]
  
  unless File.exists?("#{Chef::Config[:file_cache_path]}/#{lumisportal_file}")
    lumis_aws_s3_file "#{Chef::Config[:file_cache_path]}/#{lumisportal_file}" do
      remote_path node['lumis']['lumisportal']['remote_path']
      bucket node['lumis']['s3bucket']
      aws_access_key aws['aws_access_key_id']
      aws_secret_access_key aws['aws_secret_access_key']
      action :create
    end
  end
  
  file_extr = ""
  case lumisportal_file
  when /^.*\.zip/
    file_extr = "unzip -q "
  when /^.*\.tar.gz/
    file_extr = "tar xzf"
  end
  
  bash "Extracting LumisPortal" do
    cwd node['lumis']['lumisportal']['home']
    code <<-EOH
    #{file_extr} #{Chef::Config[:file_cache_path]}/#{lumisportal_file}
    EOH
  end
  
  package "mysql-connector-java" do
    action :install
  end
  
  bash "Correcting Setup Scripts" do
    cwd "#{node['lumis']['lumisportal']['home']}/setup"
    code <<-EOH
    cp /usr/share/java/mysql-connector-java.jar #{node['lumis']['lumisportal']['home']}/www/WEB-INF/lib/
    sed -i 's/mysql-connector-java-5.0.4-bin.jar/mysql-connector-java.jar/g' ./setclasspath.*
    sed -i 's/. setclasspath.sh/. .\\/setclasspath.sh/g' ./*.sh
    chmod +x *.sh
    EOH
  end
  
  dialect = ""
  driver_class = ""
  driver_file = ""
  
  case node['lumis']['lumisportal']['database']['type']
  when "mysql"
    dialect = "org.hibernate.dialect.MySQLDialect"
    driver_class = "com.mysql.jdbc.Driver"
    driver_file = "#{node['lumis']['lumisportal']['home']}/www/WEB-INF/lib/mysql-connector-java.jar"
  when "oracle"
    dialect = "lumis.portal.dao.hibernate.Oracle10gDialect"
    driver_class = "oracle.jdbc.driver.OracleDriver"
    driver_file = "#{node['lumis']['lumisportal']['home']}/www/WEB-INF/lib/ojdbc14.jar"
  when "sqlserver"
    dialect = "org.hibernate.dialect.SQLServerDialect"
    driver_class = "net.sourceforge.jtds.jdbc.Driver"
    driver_file = "#{node['lumis']['lumisportal']['home']}/www/WEB-INF/lib/jtds-1.2.2.jar"
  end
  
  template "#{node['lumis']['lumisportal']['home']}/lumisdata/config/lumishibernate.cfg.xml" do
    source "#{node['lumis']['lumisportal']['version']}-lumishibernate.cfg.xml.erb"
    mode "0644"
    owner "root"
    group "root"
    variables(
      :dbtype => node['lumis']['lumisportal']['database']['type'],
      :dialect => dialect,
      :driver_class => driver_class,
      :username => node['lumis']['lumisportal']['database']['username'],
      :password => node['lumis']['lumisportal']['database']['password'],
      :connectionstring => node['lumis']['lumisportal']['database']['connectionstring']
    )
  end
  
  if node['lumis']['lumisportal']['database']['initializeportal']
    bash "Executing generatesql.sh" do
      cwd "#{node['lumis']['lumisportal']['home']}/setup"
      code <<-EOH
      export JAVA_HOME=#{node['java']['java_home']}
      ./generatesql.sh
      EOH
    end
    
    lumis_lumisportal_parse_sql "Parse #{node['lumis']['lumisportal']['database']['type']} script" do
      path "#{node['lumis']['lumisportal']['home']}/setup/db_#{node['lumis']['lumisportal']['database']['type']}.sql"
      action :parse
    end
    
    executedb_file = node['lumis']['lumisportal']['executedb_url'].split('/')[-1]
    
    remote_file "#{node['lumis']['lumisportal']['home']}/setup/#{executedb_file}" do
      source node['lumis']['lumisportal']['executedb_url']
      mode 0644
      action :create_if_missing
    end
    
    template "#{node['lumis']['lumisportal']['home']}/setup/executedb.conf" do
      source "executedb.conf.erb"
      mode "0644"
      owner "root"
      group "root"
      variables(
        :username => node['lumis']['lumisportal']['database']['username'],
        :password => node['lumis']['lumisportal']['database']['password'],
        :connectionstring => node['lumis']['lumisportal']['database']['connectionstring'],
        :classname => driver_class,
        :driverfile => driver_file,
        :scriptfile => "#{node['lumis']['lumisportal']['home']}/setup/script.sql"
      )
    end
    
    bash "Creating Database Objects" do
      cwd "#{node['lumis']['lumisportal']['home']}/setup"
      code <<-EOH
      #{node['java']['java_home']}/bin/java -jar executedb.jar ./executedb.conf
      EOH
    end
    
    bash "Executing initializeportal.sh" do
      cwd "#{node['lumis']['lumisportal']['home']}/setup"
      code <<-EOH
      mkdir -p ../lumisdata/data/files
      export JAVA_HOME=#{node['java']['java_home']}
      ./initializeportal.sh
      EOH
    end
    
    bash "Creating htdocs" do
      code <<-EOH
      mkdir -p #{node['lumis']['lumisportal']['home']}/htdocs/lumis
      cd #{node['lumis']['lumisportal']['home']}/htdocs/lumis
      for i in `ls -1 #{node['lumis']['lumisportal']['home']}/www/lumis/` ; do
        if [ $i != "doc" ] ; then
          cp -rf #{node['lumis']['lumisportal']['home']}/www/lumis/$i .
        fi
      done
      find . -name *.jsp | xargs rm -rf
      chown -R #{user}: #{node['lumis']['lumisportal']['home']}/htdocs
      chmod -R +r #{node['lumis']['lumisportal']['home']}/htdocs
      EOH
    end
  end
  
  http_port = 8080 + (node['lumis']['lumisportal']['ports'] * 100)
  
  unless node['lumis']['lumisportal']['clusterconfig'].empty?
    directory "#{node['lumis']['lumisportal']['home']}/jgroups" do
      group user
      owner user
      mode "0755"
      recursive true
    end
  end
  
  template "#{node['lumis']['lumisportal']['home']}/lumisdata/config/lumisportalconfig.xml" do
    source "#{node['lumis']['lumisportal']['version']}-lumisportalconfig.xml.erb"
    mode "0644"
    owner "root"
    group "root"
    variables(
      :serverid => "#{node['fqdn']}",
      :frameworkurl => "http://#{client['url']}",
      :defaultlocale => node['lumis']['lumisportal']['defaultlocale'],
      :htmlgenerationurl => "http://localhost:#{http_port}",
      :errorpage => node['lumis']['lumisportal']['errorpage'],
      :webrootpath => "#{node['lumis']['lumisportal']['home']}/htdocs",
      :clusterconfig => node['lumis']['lumisportal']['clusterconfig'].gsub('$LUMISPORTAL_HOME', node['lumis']['lumisportal']['home'])
    )
  end
  
  template "#{node['lumis']['lumisportal']['home']}/setup/Tomcat/ROOT.xml" do
    source "ROOT.xml.erb"
    mode "0644"
    owner "root"
    group "root"
    variables(
      :context_root => "#{lumisportal_dir}/www"
    )
  end
  
  template "#{node['lumis']['lumisportal']['home']}/www/WEB-INF/jboss-web.xml" do
    source "jboss-web.xml.erb"
    mode "0644"
    owner "root"
    group "root"
  end
  
  bash "Configures LumisDataPath" do
    code <<-EOH
    sed -i 's,LUMIS_DATA_PATH,#{lumisportal_dir}/lumisdata,g' #{node['lumis']['lumisportal']['home']}/www/WEB-INF/web.xml
    EOH
  end
  
  bash "Setting Files Ownership" do
    code <<-EOH
    chown -R #{user}: #{node['lumis']['lumisportal']['home']}
    chmod -R +r #{node['lumis']['lumisportal']['home']}
    EOH
  end
end
